package com.ruoyi.rcpt.rdwj.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.rcpt.rdwj.domain.RdwjFj;
import com.ruoyi.rcpt.rdwj.domain.Rdwj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import java.io.InputStream;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.rcpt.rdwj.service.IRdwjService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 认定文件Controller
 *
 * @author huacai
 * @date 2024-11-12
 */
@RestController
@RequestMapping("/rcpt/rdwj")
public class RdwjController extends BaseController
{
    @Resource
    private IRdwjService rdwjService;

    /**
     * 查询认定文件列表
     */
    @PreAuthorize("@ss.hasPermi('rcpt:rdwj:list')")
    @GetMapping("/list")
    public TableDataInfo list(Rdwj rdwj)
    {
        startPage();
        List<Rdwj> list = rdwjService.selectRdwjList(rdwj);
        return getDataTable(list);
    }

    /**
     * 导出认定文件列表
     */
    @PreAuthorize("@ss.hasPermi('rcpt:rdwj:export')")
    @Log(title = "认定文件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Rdwj rdwj)
    {
        List<Rdwj> list = rdwjService.selectRdwjList(rdwj);
        ExcelUtil<Rdwj> util = new ExcelUtil<Rdwj>(Rdwj.class);
        util.exportExcel(response, list, "认定文件数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Rdwj> util = new ExcelUtil<Rdwj>(Rdwj.class);
        util.importTemplateExcel(response, "认定文件数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "认定文件", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('rcpt:rdwj:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<Rdwj> util = new ExcelUtil<Rdwj>(Rdwj.class);
        InputStream inputStream = file.getInputStream();
        List<Rdwj> list = util.importExcel(inputStream );
        inputStream.close();
        int count = rdwjService.batchInsertRdwj(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取认定文件详细信息
     */
    @PreAuthorize("@ss.hasPermi('rcpt:rdwj:query')")
    @GetMapping(value = "/{rdwjId}")
    public AjaxResult getInfo(@PathVariable("rdwjId") String rdwjId)
    {
        return success(rdwjService.selectRdwjByRdwjId(rdwjId));
    }

    /**
     * 新增认定文件
     */
    @PreAuthorize("@ss.hasPermi('rcpt:rdwj:add')")
    @Log(title = "认定文件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Rdwj rdwj)
    {
        rdwj.setUserId(Math.toIntExact(getUserId())); //用户ID
        rdwj.setDeptId(Math.toIntExact(getDeptId())); //部门ID
        rdwj.setCreateBy(getUsername()); //创建人

        return toAjax(rdwjService.insertRdwj(rdwj));
    }

    /**
     * 修改认定文件
     */
    @PreAuthorize("@ss.hasPermi('rcpt:rdwj:edit')")
    @Log(title = "认定文件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Rdwj rdwj)
    {
        return toAjax(rdwjService.updateRdwj(rdwj));
    }

    /**
     * 删除认定文件
     */
    @PreAuthorize("@ss.hasPermi('rcpt:rdwj:remove')")
    @Log(title = "认定文件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{rdwjIds}")
    public AjaxResult remove(@PathVariable String[] rdwjIds)
    {
        return toAjax(rdwjService.deleteRdwjByRdwjIds(rdwjIds));
    }

    /**
     * 获得认定文件主键ID (UUID)
     */
    @GetMapping("/getRdWjId")
    public AjaxResult getRdWjId(){
        String rdWj = IdUtils.fastSimpleUUID();
        return success(rdWj);
    }

    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertrdwjFjByrcptId(@RequestBody RdwjFj rdwjFj){
        return toAjax(rdwjService.insertrdwjFjByrcptId(rdwjFj));
    }
    /**
     * 根据ID查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public AjaxResult selectRdwjFjList(String sId){
        RdwjFj rdwjFj = new RdwjFj();
        rdwjFj.setRdwjId(sId);
        return AjaxResult.success(rdwjService.selectRdwjFjList(rdwjFj));
    }

    /**
     * 删除认定文件附件
     */
    @Log(title = "认定文件附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/fj/{fjIds}")
    public AjaxResult remove(@PathVariable Integer[] fjIds)
    {
        return toAjax(rdwjService.deleteRdwjFjByFjIds(fjIds));
    }

}
